package cn.mw.cmdb.view;

import cn.mw.cmdb.entity.ModelGroupInfo;
import cn.mw.cmdb.entity.ModelInfo;
import cn.mw.microMonitorCommon.api.cmdb.TableIdEnum;
import cn.mw.microMonitorCommon.api.cmdb.TreeView;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
@Data
public class TreeViewObject implements TreeView {
    private String itemName;
    private String id;
    private Object parentId;
    private int count;
    private String itemAssetsStatus;

    private List<String> childStatus;

    private String type;

    private String viewType;

    private String headerType;

    private String treePath;

    private List<TreeView> childs;

    private Integer sortNum;


    @Override
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public Object getParentId() {
        return parentId;
    }

    @Override
    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public void setParentId(Object parentId) {
        this.parentId = parentId;
    }

    public void addTreeView(TreeView treeView) {
        if (null == childs) {
            childs = new ArrayList<>();
        }
        childs.add(treeView);
    }

    public List<TreeView> getChilds() {
        return childs;
    }

    @Override
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String getViewType() {
        return viewType;
    }

    public void setViewType(String viewType) {
        this.viewType = viewType;
    }

    public void headerTypeSetting(TreeView treeView, ViewConvertContext context, String baseGroupId, String videoGroupId) {
        if (null != context) {
            if (TableIdEnum.model.getCode().equals(context.getSelectDropType())) {
                if (treeView instanceof ModelInfo) {//树节点为模型数据
                    List<String> groupPath = ((ModelInfo) treeView).getGroupPath();
                    if (groupPath != null && groupPath.contains(baseGroupId)) {
                        this.setHeaderType(TableIdEnum.instance.getCode());//固定实例表头+自定表头
                    } else {
                        this.setHeaderType(TableIdEnum.customInstance.getCode());//自定表头
                    }
                }
                if (treeView instanceof ModelGroupInfo) {
                    this.setHeaderType(TableIdEnum.model.getCode());//固定模型表头
                }
            } else if (TableIdEnum.finance.getCode().equals(context.getSelectDropType())) {
                this.setHeaderType(context.getSelectDropType());
                if (TableIdEnum.model.getCode().equals(this.getType())) {
                    this.setHeaderType(TableIdEnum.financeModel.getCode());
                }
            } else {
                this.setHeaderType(context.getSelectDropType());
                if (TableIdEnum.model.getCode().equals(this.getType())) {
                    List<String> groupPath = ((ModelInfo) treeView).getGroupPath();
                    // 视频模型列表展示需要使用自定表头
                    if (groupPath != null && groupPath.contains(videoGroupId)) {
                        this.setHeaderType(TableIdEnum.customInstance.getCode());//自定表头
                    } else {
                        this.setHeaderType(TableIdEnum.commonInstance.getCode());
                    }
                }
            }
        }
    }

    /**
     * ，从树结构中移除指定类型节点
     */
    public void removeChildByType(String type) {
        if (childs != null) {
            Iterator<TreeView> iterator = childs.iterator();
            while (iterator.hasNext()) {
                TreeView next = iterator.next();
                if (next instanceof TreeViewObject) {
                    TreeViewObject child = (TreeViewObject) next;
                    if (type.equals(child.getType())) {
                        iterator.remove(); // 移除指定类型节点
                    } else {
                        child.removeChildByType(type); // 递归处理子节点的 childList
                    }
                }
            }
        }
    }

}
